package cn.stylefeng.guns.modular.comm.service.impl;

import cn.hutool.core.date.DateTime;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.comm.entity.News;
import cn.stylefeng.guns.modular.comm.mapper.NewsMapper;
import cn.stylefeng.guns.modular.comm.model.params.NewsParam;
import cn.stylefeng.guns.modular.comm.model.result.NewsResult;
import cn.stylefeng.guns.modular.comm.service.NewsService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 新闻通知 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-18
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Override
    public void add(NewsParam param) {
        News entity = getEntity(param);
        if (entity.getContentText().length() > 50) {
            entity.setContentText(entity.getContentText().substring(0, 50));
        }
        entity.setInsertTime(new DateTime());
        entity.setInsertUser(ShiroKit.getUser().getId());
        this.save(entity);
    }

    @Override
    public void delete(NewsParam param) {
        News news = this.getById(getKey(param));
        news.setDelFlag(true);
        news.setUpdateUser(ShiroKit.getUser().getId());
        news.setUpdateTime(new DateTime());
        this.updateById(news);
    }

    @Override
    public void update(NewsParam param) {
        News oldEntity = getOldEntity(param);
        News newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdateUser(ShiroKit.getUser().getId());
        newEntity.setUpdateTime(new DateTime());
        if (newEntity.getContentText()!=null&&newEntity.getContentText().length() > 50) {
            newEntity.setContentText(newEntity.getContentText().substring(0, 50));
        }
        this.updateById(newEntity);
    }

    @Override
    public NewsResult findBySpec(NewsParam param) {
        return null;
    }

    @Override
    public List<NewsResult> findListBySpec(NewsParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NewsParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<News> findDisplayNews() {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", "0").eq("display_flag", "0").orderByAsc("title");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<NewsResult> findTop2News(String type) {
        return this.baseMapper.findTop2News(type);
    }

    @Override
    public List<NewsResult> findNews(NewsParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findNews(pageContext, param);
        return page.getRecords();
    }

    private Serializable getKey(NewsParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private News getOldEntity(NewsParam param) {
        return this.getById(getKey(param));
    }

    private News getEntity(NewsParam param) {
        News entity = new News();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
